package neuronvisualizer;

/**

 * ParseNeurons class
 * @author  John Newsom
 * 
 * This class will parse the data from the neurons.txt file
 * and then output an excerpt of java code that will construct
 * a hashmap of all the neurons, enabling us to refer to the 
 * neurons by their IDs in the simulator.
 */
 
 import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

import com.threed.jpct.SimpleVector;
 
 
 class ParseNeurons{
	private Visual parent;
	private String readPath;
	private float aveX;
	private float aveY;
	private float aveZ;
	
	public Float maxX=null;
	public Float minX=null;
	public Float maxY=null;
	public Float minY=null;
	public Float maxZ=null;
	public Float minZ=null;
	
	public ParseNeurons(Visual parent,String f){
		this.parent = parent;
		readPath=f;
		beginParse();
	}
	
	public void beginParse(){	
		File readFrom = new File(readPath);
		BufferedReader reader = null;
		String inLine="";
		
		try{
			reader = new BufferedReader(new FileReader(readFrom));
			inLine = reader.readLine();
		} catch(Exception e){}
		
		StringTokenizer st;
		
		/**
		* The following variables are the components that make up a neuron for the simulator
		*/
		int ID = 0;
		String neuronType = "";
		String layer = "";
		Float xPos = 0f;
		Float yPos = 0f;
		Float zPos = 0f;	
		
		int totalX=0;
		int totalY=0;
		int totalZ=0;
		
		ArrayList<Integer> connections;
				
		while(inLine!=null){
			try{
				connections = new ArrayList<Integer>();
				st = new StringTokenizer(inLine);
				
				ID = Integer.parseInt(st.nextToken());
				neuronType = st.nextToken();
				layer = st.nextToken();
				xPos = Float.parseFloat(st.nextToken())/2;
				yPos = (Float.parseFloat(st.nextToken()) * -1)/2;
				zPos = Float.parseFloat(st.nextToken())/2;
				
				
				if(maxX==null || maxX.compareTo(xPos)<0){maxX=xPos;}
				if(minX==null || minX.compareTo(xPos)>0 ){minX=xPos;}				
				//MAXY AND MINY ARE SWITCHED BECAUSE THE 3D LIBRARY USES NEGATIVE Y VALUES TO GO UP
				if(maxY==null || maxY.compareTo(yPos)>0 ){maxY=yPos;}
				if(minY==null || minY.compareTo(yPos)<0 ){minY=yPos;}
				if(maxZ == null || maxZ.compareTo(zPos)<0){maxZ=zPos;}
				if(minZ == null || minZ.compareTo(zPos)>0){minZ=zPos;}
				
				totalX+=xPos;
				totalY+=yPos;
				totalZ+=zPos;
				/**
				 * Adds all the remaining values in the readLine to the list of connections
				 */
				/*while(st.hasMoreTokens()){
					connections.add((Integer)Integer.parseInt(st.nextToken()));
				}*/
				
				
				/**
				 * Starts building a temporary neuron which will then be added to the
				 * list of neurons in Simulator parent.
				 */
				
				Neuron tempNeuron = new Neuron(ID,neuronType,layer,xPos.intValue(),yPos.intValue(),zPos.intValue());
				
				
				/*for(int i=0;i<connections.size()-1;i++){
					tempNeuron.addConnection(new Connection(ID,(int)connections.get(i)));
				}*/
				
				parent.addNeuron(tempNeuron);
				
				inLine=reader.readLine();
			}catch(Exception e){System.out.println(""+e.toString());}
		}

		int total= parent.neuronCount();
		
		aveX=totalX/total;
		aveY=totalY/total;
		aveZ=totalZ/total;
		
		try{
			reader.close();
		}catch(Exception e){}
		
		
		
	}
	
	public SimpleVector midPoint(){return new SimpleVector(aveX,aveY,aveZ);}
 }//END CLASS PARSENEURONS